/*
 * File: exercise11.cpp
 * -------------------
 * isSentencePalindrome function 
 * compile command :
 * g++ -Wall -g exercise11.cpp -o exercise11 -lStanfordCPPLib -I ../include/
 */

#include <iostream>
#include <string>
#include <cctype>
#include "simpio.h"
using namespace std;

bool isSentencePalindrome(string str);
string preprocess(string str);

int main()
{
	string str ;
	while ((str = getLine("Enter a sentence: "))!="")
	{
		string strpreprocessing = preprocess(str);

		if(isSentencePalindrome(strpreprocessing))
			cout<<"That sentence is a palindrome."<<endl;
		else
			cout<<"That sentence is not a palindrome."<<endl;
	}
	return 0;
}

string preprocess(string str)
{
	string result = "";
	int nChars = str.length();
	for(int i = 0; i < nChars; i++)
	{
		if(isalpha(str[i]))
		{
			if(isupper(str[i]))
				str[i] = tolower(str[i]);
			result += str[i];	
		}
	}	
	return result;
}
bool isSentencePalindrome(string str)
{
	/* All characters of length 0 or length 1 are guaranteed to
	 * be palindromes.
	 */
	if (str.length() <= 1)
	{
		return true;
	}
	/* If the first and last character of the string aren't the same,
	 * the string cannot be a palindrome.
	 */
	else if (str[0] != str[str.length() - 1])
	{
		return false;
	}
	/* Otherwise, this string is a palindrome precisely when the middle
	 * characters are a palindrome.
	 */
	else
	{
		return isSentencePalindrome(str.substr(1, str.length() - 2));
	}

	return true;
}

